<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="card-title">
        <span>信息发送</span>
      </div>
      <div class="msg-box">
        <el-form
          :model="msgForm"
          :rules="rules"
          ref="msgForm"
          label-width="80px"
          class="demo-msgForm"
        >
          <el-form-item label="消息标题" prop="title">
            <el-input
              type="text"
              v-model="msgForm.title"
              maxlength="20"
              show-word-limit
              style="width: 250px"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="接收对象"
            prop="group_index"
            style="width: 260px"
          >
            <el-select
              v-model="msgForm.group_index"
              placeholder="请选择要发送的对象"
              @change="Onchange"
            >
              <el-option label="用户组" value="0"></el-option>
              <el-option label="管理组" value="1"></el-option>
              <el-option label="全体用户" value="2"></el-option>
              <el-option label="单用户" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="is_single" label="用户ID" prop="rec_id">
            <el-input v-model="msgForm.rec_id" style="width: 150px"></el-input>
          </el-form-item>
          <el-form-item label="消息内容" prop="content">
            <el-input
              type="textarea"
              rows="4"
              v-model="msgForm.content"
              maxlength="200"
              show-word-limit
              style="width: 100%"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('msgForm')"
              >发送</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      msgForm: {
        // 标题
        title: "",
        // 群发信息的用户组，可以为空
        group: "",
        // 单个用户发送的时候
        rec_id: "",
        // 内容
        content: "",
        // 选择的信息组的索引（记录列表下拉的索引）
        group_index: "",
      },
      // 是否是发送过单用户
      is_single: "",
      rules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        group_index: [
          { required: true, message: "接接收对象不能为空", trigger: "change" },
        ],
        rec_id: [
          { required: true, message: "用户ID不能为空", trigger: "blur" },
        ],
        // min最小不能小于6位
        content: [
          { min: 6, required: true, message: "最少输入6个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {},

  methods: {
    ...mapActions({
      msgSend: "message/msgSend",
    }),
    // 当用户接收组选择器发生变化时的回调
    Onchange(index) {
      console.log(index);
      if (index == 3) {
        this.is_single = true;
        this.msgForm.group = "";
      } else {
        this.is_single = false;
        this.msgForm.rec_id = "";
        this.msgForm.group = index;
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.msgSend(this.msgForm).then((res) => {
            this.$message({
              message: "消息发送成功",
              type: "success",
            });
            this.resetForm(formName)
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  .card-title {
    font-size: 22px;
  }
}
</style>
